//
//  SearchHomeViewController.m
//  搜索功能
//
//  Created by Hello Cai on 2020/10/9.
//  Copyright © 2020 Hello Cai. All rights reserved.
//

#import "SearchHomeViewController.h"
#import "SearchNavigationView.h"
#import "SearchHistoryView.h"
#import "SearchResultWebViewController.h"

NSString *const KeywordSearchHistory = @"KeywordSearchHistory";

@interface SearchHomeViewController ()
@property(nonatomic, strong)NSMutableArray *searchArray;
@property(nonatomic, strong)SearchNavigationView *searchNav;
@property(nonatomic, strong)SearchHistoryView *historyView;
@property(nonatomic, strong)SearchResultWebViewController *webVC;
@property(nonatomic, strong)UIView *containerView;

@property(nonatomic, assign)BOOL didSearched;   //是否已经搜索
@end

@implementation SearchHomeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.view sendSubviewToBack:self.navigationController.navigationBar];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    NSLog(@"%@", [self.navigationController.childViewControllers firstObject]);
    [[[self.navigationController.childViewControllers firstObject] view] addSubview:self.navigationController.navigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:KeywordSearchHistory];
    [self.searchArray addObjectsFromArray:array];
    
    [self setView];
}

-(void)setView{
    SearchNavigationView *searchNav = [[SearchNavigationView alloc] initWithFrame:CGRectMake(0, 20, kScreenW, 44)];
    [self.view addSubview:searchNav];
    self.searchNav = searchNav;
    
    __weak typeof(self) weakSelf = self;
    //返回键
    searchNav.navBackBlock = ^{
        [weakSelf.navigationController popViewControllerAnimated:NO];
    };
    //搜索内容
    searchNav.searchTextBlock = ^(NSString * _Nonnull searchText) {        [weakSelf historySearcWithText:searchText];
    };
    //搜索框的获得焦点和失去焦点
    searchNav.editingTFBlock = ^(BOOL isEditing) {
        //更新UI
        weakSelf.historyView.hidden = !isEditing;
    };
    
    
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64)];
    [self.view addSubview:containerView];
    self.containerView = containerView;
    
    SearchResultWebViewController *webVC = [[SearchResultWebViewController alloc] init];
    [self addChildViewController:webVC];
    self.webVC = webVC;

    
    SearchHistoryView *historyView = [[SearchHistoryView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) textArray:self.searchArray];
    historyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:historyView];
    self.historyView = historyView;
    //清除所有搜索记录
    historyView.cleanClickBlock = ^{
        [weakSelf cleanHistory];
    };
    //点击历史文本
    historyView.textClickBlock = ^(NSString * _Nonnull text) {
        [weakSelf historySearcWithText:text];
    };
}

-(void)historySearcWithText:(NSString *)text{
    //设置或调整history文本的搜索顺序
    [self saveSearchText:text];
    
    //更新UI
    [self.searchNav searchText:text];
    self.historyView.hidden = YES;
    if (self.didSearched == NO) {//首次搜索
        self.didSearched = YES;
        self.webVC.view.frame = self.containerView.bounds;
        [self.containerView addSubview:self.webVC.view];
    }else{
        
    }
    [self.searchNav searchText:text];
    [self updateRecentSearchOrderUI];
}

//清理所有搜索记录
-(void)cleanHistory{
    NSString *title = @"确定删除全部搜索记录?";
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if (self.historyView.textArray.count > 0) {
            [self.historyView.scrollView removeFromSuperview];
            [self.historyView.textArray removeAllObjects];
        }
        
        [self.searchArray removeAllObjects];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:KeywordSearchHistory];
    }];
    [alertVC addAction:okAction];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

-(void)saveSearchText:(NSString *)searchText{
    if ([self.searchArray containsObject:searchText]) {
        [self.searchArray removeObject:searchText];
    }
    [self.searchArray addObject:searchText];
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:KeywordSearchHistory];
    [[NSUserDefaults standardUserDefaults] setObject:self.searchArray forKey:KeywordSearchHistory];
}

-(void)updateRecentSearchOrderUI{
    self.historyView.textArray = [self.searchArray mutableCopy];
    [self.historyView updateOrder];
}

-(NSMutableArray *)searchArray{
    if (!_searchArray) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}
@end
